package com.scoy.honeymei.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.TripDateBean;
import com.scoy.honeymei.fragment.home.Trip0Fragment;
import com.scoy.honeymei.fragment.home.TripFragment;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TripActivity$
 * @Description: 出行界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 上午 8:57$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 上午 8:57$
 * @UpdateRemark: 更新内容
 */
public class Trip0Activity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.taa_tab)
    TabLayout taaTab;
    @BindView(R.id.taa_vp)
    ViewPager taaVp;
    private Trip0Activity mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trip0);
        ButterKnife.bind(this);

        mContext = Trip0Activity.this;

        initNormal();

        httpDate();

    }

    @Override
    public void initNormal() {
        titleTv.setText(R.string.travelout);

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    private void initTab(List<TripDateBean> data) {
        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTitles[i] = data.get(i).getTime();
            fragments.add(Trip0Fragment.newInstance(i, data.get(i).getStarttime()));
        }

        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments, mTitles);
        taaTab.setupWithViewPager(taaVp, false);
        taaVp.setAdapter(tabFragmentAdapter);
        taaVp.setCurrentItem(0);
    }

    @OnClick({R.id.back_iv, R.id.title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_tv:

                break;
        }
    }

    private void httpDate() {
        HttpParams params = new HttpParams();
        HpGo.newInstance().HttpGo(this, MyUrl.TRIP_DATE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<TripDateBean> data = new Gson().fromJson(o, new TypeToken<List<TripDateBean>>() {
                }.getType());
                initTab(data);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
